package com.nail.api.customMake.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.nail.api.customMake.bean.FileUploadInfo;
import com.nail.api.customMake.bean.PhoneModel;
import com.nail.api.customMake.dao.IFileUploadDao;
import com.nail.api.customMake.dao.IPhoneModelDao;
import com.nail.api.customMake.service.IPhoneModelService;
import com.nail.service.impl.BaseService;
import com.nail.utils.PageResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by jie on 2017/9/17.
 */
@Service("phoneModelService")
public class PhoneModelServiceImpl extends BaseService implements IPhoneModelService{
    Logger logger = Logger.getLogger(this.getClass());


    @Autowired
    IPhoneModelDao phoneModelDao;

    @Autowired
    private IFileUploadDao fileUploadDao;

    @Transactional
    public void addPhoneModel(PhoneModel phoneModel){
        phoneModelDao.insertSelective(phoneModel);
    }

    @Transactional
    public void updatePhoneModel(PhoneModel phoneModel){
        phoneModelDao.updateByPrimaryKeySelective(phoneModel);
    }

    @Transactional
    public void deletePhoneModel(Long id){
        phoneModelDao.deleteByPrimaryKey(id);
    }

    public List<PhoneModel> getPhoneModels(PageResult pageResult){
        return phoneModelDao.getPhoneModels(pageResult.getBegin(),pageResult.getPageSize());
    }

    @Transactional
    public void setSort(Long id,Integer sort){
        phoneModelDao.setSort(id,sort);
    }

    @Transactional
    public void setIsAble(Long id,Integer isAble){
        phoneModelDao.setIsAble(id,isAble);
    }

    public PhoneModel getPhoneModelById(Long id){
        PhoneModel phoneModel =  phoneModelDao.selectByPrimaryKey(id);
        if(phoneModel != null && !StringUtils.isEmpty(phoneModel.getPhoneImgId()) && !StringUtils.isEmpty(phoneModel.getModelImgId()) ){
            List<FileUploadInfo> fileUploadInfos = fileUploadDao.getFileUploadInfoByIds(phoneModel.getPhoneImgId()+","+phoneModel.getModelImgId());
            phoneModel.setPhoneImg(fileUploadInfos.get(0));
            phoneModel.setModelImg(fileUploadInfos.get(1));
        }
        return phoneModel;
    }
}
